package com.ynet.hmds28.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.alibaba.druid.util.StringUtils;
import com.ynet.hm.user.service.QueryCanSubService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hmds28.user.dao.ServiceconsumeDao;
import com.ynet.hmds28.user.dao.ServiceprovideDao;
import com.ynet.hmds28.user.model.Serviceprovide;
import com.ynet.hmds28.user.model.extend.FieldClass;
import com.ynet.hmds28.user.model.extend.ServiceProvideExtend;
import com.ynet.hmds28.user.redis.RedisService;
@Transactional
@HSFProvider(serviceInterface = QueryCanSubService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class QueryCanSubServiceImpl implements QueryCanSubService {
	@Autowired
	private ServiceprovideDao serviceprovideDao;
	@Autowired	
	private ServiceconsumeDao serviceconsumeDao;
	@Autowired
	private RedisService redisService;
	@Override
	public Result<?> queryCanSubService(String userId,String pageNo,String pageSize) {
		/*//查询所有的服务
				ServiceprovideQuery sq=new ServiceprovideQuery();
				sq.createCriteria();
				List<Serviceprovide> sps=serviceprovideDao.selectByExample(sq);
				//查询用户已经订阅的服务
				ServiceconsumeQuery ssq=new ServiceconsumeQuery();
				ssq.createCriteria().andUseridEqualTo(userId);
				List<Serviceconsume> sss=serviceconsumeDao.selectByExample(ssq);
				//用户可订阅服务集合
				List<Serviceprovide>  provices=new ArrayList<>();
				if(!CollectionUtils.isEmpty(sps)){
					for(Serviceprovide s:sps){
						boolean flag=true;
						if(!CollectionUtils.isEmpty(sss)){
							for(Serviceconsume ss:sss){
								if(s.getSeviceproviceid().equals(ss.getSeviceproviceid())){
									flag=false;
									break;
								}
							}
						}
						if(flag){
							provices.add(s);
						}
					}
				}*/
			if(StringUtils.isEmpty(pageNo)){
				pageNo="1";
			}
			if(StringUtils.isEmpty(pageSize)){
				pageSize="10";
			}
			List<Serviceprovide>  provices = serviceprovideDao.queryCanSubService(userId, Integer.parseInt(pageNo)-1, Integer.parseInt(pageSize));
			int totalNum=serviceprovideDao.queryCanSubServiceNum(userId);
			
			List<ServiceProvideExtend> spes=new ArrayList<>();
			if(!CollectionUtils.isEmpty(provices)){
				for(Serviceprovide sp:provices){
					ServiceProvideExtend spe=new ServiceProvideExtend();
					spe.setSeviceproviceid(sp.getSeviceproviceid());
					//从redis中取流量控制数
					String flowControl=(String) redisService.get(sp.getSeviceproviceid());
					spe.setFlowControl(flowControl);
					//查询输入字段
					List<FieldClass> importString=new ArrayList<>();
					String importField=sp.getImportfield();
					if(!StringUtils.isEmpty(importField)){
						String[] strs=importField.split(",");
						for(int i=0;i<strs.length;i++){
							FieldClass fc=new FieldClass();
							String[] str=strs[i].split("-");
							fc.setEnglishName(str[0]);
							fc.setChineseName(str[1]);
							importString.add(fc);
						}
					}
					spe.setImportField(importString);
					//查询输出字段
					List<FieldClass> exportString=new ArrayList<>();
					String exportField=sp.getExportfield();
					if(!StringUtils.isEmpty(exportField)){
						String[] strs=exportField.split(",");
						for(int i=0;i<strs.length;i++){
							FieldClass fc=new FieldClass();
							String[] str=strs[i].split("-");
							fc.setEnglishName(str[0]);
							fc.setChineseName(str[1]);
							exportString.add(fc);
						}
					}
					spe.setExportField(exportString);
					spe.setServicename(sp.getServicename());
					spe.setServicedesc(sp.getServicedesc());
					spe.setServicecostrule(sp.getServicecostrule());
					spes.add(spe);
				}
			}
			Map<String,Object> m=new HashMap<>();
			m.put("totalNum", totalNum);
			m.put("serviceProvide", spes);
			return ResultUtil.success(m);
	}

}
